/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import org.heresylabs.rostercomposer.actions.LoadArmyAction;
import org.heresylabs.rostercomposer.storage.RosterStorage;
import org.heresylabs.rostercomposer.units.Army;
import org.heresylabs.rostercomposer.units.ArmyReader;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class LoadDialog extends JDialog {

    private final ArmyFile[] KNOWN_ARMIES = {new ArmyFile("Blood Angels", "/BloodAngels.xml"), new ArmyFile("Chaos Space Marimes", "/ChaosSpaceMarines.xml"), new ArmyFile("Dark Angels", "/DarkAngels.xml")};
    private final JList armiesList;

    public LoadDialog() {
        super((JDialog) null, "Roster Composer", true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        armiesList = new JList(KNOWN_ARMIES);
        armiesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        armiesList.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() != 2) {
                    return;
                }
                openArmy();
            }

        });
        JScrollPane scrollPane = new JScrollPane(armiesList);

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 0, 6, 0));
        JButton okButton = new JButton(new OkAction());
        buttonsPanel.add(okButton);
        buttonsPanel.add(new JButton(new OpenAction()));
        buttonsPanel.add(new JButton(new LoadArmyAction(this)));
        buttonsPanel.add(new JButton(new CancelAction()));
        buttonsPanel.setMaximumSize(buttonsPanel.getPreferredSize());

        Box bottomPanel = Box.createHorizontalBox();
        bottomPanel.add(Box.createHorizontalGlue());
        bottomPanel.add(buttonsPanel);

        JPanel contentPane = new JPanel(new BorderLayout(6, 6));
        contentPane.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
        contentPane.add(scrollPane, BorderLayout.CENTER);
        contentPane.add(bottomPanel, BorderLayout.SOUTH);
        super.setContentPane(contentPane);
        super.getRootPane().setDefaultButton(okButton);

        super.pack();
        super.setLocationRelativeTo(null);
        super.setVisible(true);
    }

    private void openArmy() {
        ArmyFile af = (ArmyFile) armiesList.getSelectedValue();
        if (af == null) {
            return;
        }
        super.dispose();
        Army army = new ArmyReader().readArmy(af.getResource());
        new ComposerFrame(army);
    }

    private class ArmyFile {

        private final String name;
        private final String resource;

        public ArmyFile(String name, String resource) {
            this.name = name;
            this.resource = resource;
        }

        public String getName() {
            return name;
        }

        public String getResource() {
            return resource;
        }

        @Override
        public String toString() {
            return name;
        }

    }

    private class CancelAction extends AbstractAction {

        public CancelAction() {
            putValue(NAME, "Cancel");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            LoadDialog.this.dispose();
        }

    }

    private class OkAction extends AbstractAction {

        public OkAction() {
            putValue(NAME, "Ok");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            openArmy();
        }

    }

    private class OpenAction extends AbstractAction {

        public OpenAction() {
            putValue(NAME, "Open");
            putValue(SHORT_DESCRIPTION, "Open saved roster");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            RosterStorage.openRoster();
            LoadDialog.this.dispose();
        }

    }

}
